package com.suliao.web.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suliao.common.annotation.LoginPass;
import com.suliao.common.base.PageFactory;
import com.suliao.common.base.Result;
import com.suliao.common.base.UserContext;
import com.suliao.common.util.CryPtoUtil;
import com.suliao.common.util.SessionUtil;
import com.suliao.home.entity.content.InformationDO;
import com.suliao.home.entity.content.InformationTypeDO;
import com.suliao.home.service.content.InformationService;
import com.suliao.home.service.content.InformationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.apache.dubbo.config.annotation.Reference;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：FeifeiYu
 * @date ：Created in 2020/12/14 15:40
 * @description： 产业资讯
 * @version: 1.0
 */

@Controller
@RequestMapping(value = "/industryInfo")
@LoginPass
public class IndustryInfoController {

    @Reference
    private InformationService industryInfoService;

    @Reference
    private InformationTypeService industryInfoTypeService;

    /**
     * 共享工具类
     */
    @Autowired
    private SessionUtil sessionUtil;

    /**
     * 获取产业资讯的信息列表
     *
     * @param columnNames
     * @param page
     * @param uuid
     * @param session
     * @return
     */
    @GetMapping(value = "/infoList")
    public String getIndustryInformationList(Model mv, @RequestParam(value = "columnNames", defaultValue = "") String columnNames,
                                             Integer page, String uuid,
                                             HttpSession session) {
        try {
            //1、获取登录人信息
            UserContext user = sessionUtil.getUserContext();
            mv.addAttribute("userinfo", user);

            //2、判断是否登录
            //查询登录用户的塑金余额
            if (user != null) {
                Integer corpId = user.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (corpId == null||corpId==0) {
                    mv.addAttribute("plasticGoldList", plasticGoldList);
                } else {
                    //plasticGoldList = industryExhibitorService.findAllPlasticGolds(corpId);
                    mv.addAttribute("plasticGoldList", plasticGoldList);
                }
            }
            //用于首页样式选中样式回显
            mv.addAttribute("backName", "产业资讯");
            mv.addAttribute("exhibitionName", "产业资讯");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "home/industryInformation";
    }

    /**
     * 产业资讯点击量+1
     * @param id 产业资讯id
     * @return
     */
    @PostMapping("/updateClickRate")
    @ResponseBody
    public Result updateClickRate(Integer id) {
        Result result = null;
        try {
            //产业资讯点击量加一
            int count = industryInfoService.updateIndustryInfoHits(id);

            //返回请求
            if (count > 0) {
                result = Result.ok();
            } else {
                result = Result.error("资讯点击量增加失败");
            }

        } catch (Exception e) {
            result = Result.error("服务器异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 产业资讯点赞量+1
     *
     * @return
     */
    @PostMapping("/updatecoolNum")
    @ResponseBody
    public Result updateLikeNum(Integer id, Integer number) {
        Result result = null;
        try {
            //产业资讯点赞或者取消点赞
            Integer count = industryInfoService.updateLikeNum(id,number);

            //返回请求
            if (count > 0) {
                result = Result.ok();
            } else {
                result = Result.error("资讯点赞数失败");
            }
        } catch (Exception e) {
            result = Result.error("服务器异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 局部刷新
     *
     * @return
     */
    @GetMapping("/getInfoList")
    @ResponseBody
    public Result getInfoList(Integer pageNum,
                              String columnNames,
                              Integer id) {
        Result result = Result.ok();
        try {
            //返回map
            Map res = new HashMap();

            //每页显示多少条
            Integer pageSize = 5;
            //当前页数
            Integer current = (pageNum == null || pageNum == 0) ? 1 : pageNum;

            //根据所属栏目类别ID获取产业资讯类型信息
            List<InformationTypeDO> infoTypeList = industryInfoTypeService.getIndustryInfoType();
            res.put("sortInfoList", infoTypeList);

            // 产业资讯列表数据刷新
            Page<InformationDO> industryInfoList = industryInfoService.loadIndustryInfoList(PageFactory.createPageInfo(current,pageSize),id);

            //封装数据
            res.put("pageList", industryInfoList.getRecords());

            // 总记录数
            res.put("allNum", industryInfoList.getTotal());
            //每页显示多少条
            res.put("pageS", industryInfoList.getSize());
            //当前页数
            res.put("pageNum", pageNum);
            res.put("columnNames", columnNames);
            result.setData(res);
        } catch (Exception e) {
            result = Result.error("服务器异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过产业资讯的所属栏目类别ID，去查询资讯列表信息
     *
     * @return
     */
    @GetMapping("/queryInfoById")
    @ResponseBody
    public Result queryInfoById(Integer id) {
        Result result = Result.ok();
        try {
            List<InformationDO> industryInfoList = industryInfoService.queryInfoByTypeId(id);
            result.setData(industryInfoList);
        } catch (Exception e) {
            result = Result.error("服务器异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取产业资讯详情页展示信息
     * @param mv
     * @param id
     * @return
     */
    @GetMapping(value = "/induArtDetail")
    public String getIndustryInfoDetail(Model mv ,Integer id) {
        try {
            //通过id查询产业资讯
            InformationDO infoDo =  industryInfoService.getIndustryInfoById(id);
            mv.addAttribute("induInfo",infoDo);
            mv.addAttribute("message",infoDo.getInformationBody());

            // 获取登录人信息
            UserContext user = sessionUtil.getUserContext();

            //获取塑金
           /* if(user == null){
                System.out.println("----------");
            }else{
                Integer corpId = user.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if(corpId == null||corpId == 0){
                    mv.addAttribute("plasticGoldList",plasticGoldList);
                }else{
                    EnterpriseInfoDto enterpriseInfoDto = new EnterpriseInfoDto();
                    enterpriseInfoDto.setUuid(corpId);
                    plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);
                    mv.addAttribute("plasticGoldList",plasticGoldList);
                }
            }*/
            //用于首页样式选中样式回显
            String backName = "产业资讯";
            mv.addAttribute("backName",backName);
            mv.addAttribute("exhibitionName","产业资讯");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "home/industryArtDetail";
    }

    @GetMapping(value = "/induArtrealTimeDetail")
    public String getInduArtrealTime(Model mv,Integer id){
        try {
            InformationDO infoDo =  industryInfoService.getIndustryInfoById(id);
            mv.addAttribute("artList",infoDo);
            mv.addAttribute("messageBody",infoDo.getInformationBody());
            mv.addAttribute("exhibitionName","产业资讯");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "induArtDetail/industryArtRealDetail";
    }


}

